package com.atguigu.guli.edu.controller.api;


import com.atguigu.guli.edu.entity.Course;
import com.atguigu.guli.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.edu.entity.query.CourseQuery;
import com.atguigu.guli.edu.entity.vo.CourseDetailsVo;
import com.atguigu.guli.edu.service.EduCourseService;
import com.atguigu.guli.service.base.dto.CourseInfoDto;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.result.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 课程
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-01-19 10:59:18
 */
@Api(tags = "课程信息")

@RestController
@RequestMapping("/api/edu/educourse")
public class ApiEduCourseController {
    @Autowired
    private EduCourseService eduCourseService;

    /*
    远程调用 增加课程销量
     */
    @GetMapping("/feign/{courseId}")
    public void updateCourseByCount(@PathVariable String courseId){
        //更新课程数量
        try {
            eduCourseService.updateCourseByCount(courseId);
        }catch (Exception e){
            throw new RuntimeException();
        }
    }

    //远程调用 查询课程信息
    @GetMapping("/order/{courseId}")
    public R OrderCourse(@PathVariable("courseId") String courseId){
        CourseInfoDto courseInfoDto = eduCourseService.getOrderCourse(courseId);

        return R.ok().data("data",courseInfoDto);
    }


    //根据讲师id查询他讲过的课程信息
    @ApiOperation("根据讲师id查询课程信息")
    @GetMapping("/select/{teacherId}")
    public R getByTeacherId(@PathVariable String teacherId){
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Course::getId,Course::getTitle,Course::getCover);
        queryWrapper.eq(Course::getTeacherId,teacherId);
        List<Course> list = eduCourseService.list(queryWrapper);
        return R.ok().data("data",list);
    }


    @ApiOperation("根据分类查询课程信息")
    @GetMapping("/list/course")
    public R CourseList(CourseQuery courseQuery){
        List<Course> courseQueries = eduCourseService.listCourse(courseQuery);
        return R.ok().data("data",courseQueries);
    }

    @ApiOperation("根据课程id查询详细信息")
    @GetMapping("/courseinfo/{courseid}")
    public R courseInfoById(@PathVariable String courseid){
        CourseDetailsVo courseDetailsVo = eduCourseService.getCoursesDetails(courseid);
        return R.ok().data("data",courseDetailsVo);
    }
}
